﻿using System.Web.Mvc;
using SlamCms.Areas._default.Models;
using SlamCms.Common;
using SlamCms.Web;

namespace SlamCms.Areas._admin.Controllers
{
	public class LayoutController : AdminBaseController
	{
		private readonly SlamCmsContext _slamCmsContext;

		public LayoutController(SlamCmsContext slamCmsContext)
		{
			_slamCmsContext = slamCmsContext;
		}

		[ChildActionOnly]
		public ActionResult TopBar()
		{
			var model = new LayoutTopBarViewModel()
			{
				Title = "SlamCms Administration Tools",
				User = _slamCmsContext.User
			};

			ViewData.Model = model;
			return PartialView();
		}

		[ChildActionOnly]
		public ActionResult SideBar(string selected)
		{
			var cookieManager = HttpContext.GetCookieManager();
			var model = new LayoutSideBarViewModel();

			// Dashboard
			model.AddTab("dashboard", "Dashboard")
					.WithIcon("home")
					.WithUrl(Url.Action("Index", "Dashboard"));

			// Reports
			var reportsTab = model.AddTab("manager", "Manage")
								.WithIcon("cog");
			reportsTab.AddMenuItem("urlhandler", "URL Manager", Url.Action("Index", "UrlHandler"))
								.WithIcon("table");

			if (selected.IsNullOrWhiteSpace())
			{
				var actionName = this.ControllerContext.ParentActionViewContext.RouteData.Values["action"].ToString();
				if (!model.SetSelected(actionName))
				{
					var controllerName = this.ControllerContext.ParentActionViewContext.RouteData.Values["controller"].ToString();
					model.SetSelected(controllerName);
				}
			}
			else
			{
				if (!model.SetSelected(selected))
					model.SetSelected("dashboard");
			}
			

			ViewData.Model = model;
			return PartialView();
		}
	}
}
